package com.cmc.cloud.cmclink.doc.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.vo.guaranteelettervo.GuaranteeLetterCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.guaranteelettervo.GuaranteeLetterPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.guaranteelettervo.GuaranteeLetterRespVO;
import com.cmc.cloud.cmclink.doc.vo.guaranteelettervo.GuaranteeLetterUpdateReqVO;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;


import com.cmes.framework.common.pojo.PageResult;

import com.cmc.cloud.cmclink.doc.entity.GuaranteeLetterDO;
import com.cmc.cloud.cmclink.doc.service.GuaranteeLetterService;


/**
 * {table.classComment}前端接口Controller
 * 
 * @author codegen
 */
@Tag(name = "管理后台 - 长期保函")
@RestController
@RequestMapping(UrlConstants.APPLICATION_URL+"/letter")
@Validated
public class GuaranteeLetterController {

    @Resource
    private GuaranteeLetterService guaranteeLetterService;

   
    @Operation(summary = "创建长期保函")
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission('doc:maintainLongTermGuarantee:add')")
    public CommonResult<Void> createGuaranteeLetter(@Valid @RequestBody GuaranteeLetterCreateReqVO createReqVO) {
        guaranteeLetterService.createGuaranteeLetter(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新长期保函")
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission('doc:maintainLongTermGuarantee:edit')")
    public CommonResult<Void> updateGuaranteeLetter(@Valid @RequestBody GuaranteeLetterUpdateReqVO updateReqVO) {
        guaranteeLetterService.updateGuaranteeLetter(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除长期保函")
    @Parameter(name = "id", description = "编号", required = true)
    @GetMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPermission('doc:maintainLongTermGuarantee:delete')")
    public CommonResult<Void> deleteGuaranteeLetter(@PathVariable("id") Long id) {
        guaranteeLetterService.deleteGuaranteeLetter(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得长期保函")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @GetMapping("/getById/{id}")
    public CommonResult<GuaranteeLetterRespVO> getGuaranteeLetter(@PathVariable("id") Long id) {
        GuaranteeLetterDO guaranteeLetter = guaranteeLetterService.getGuaranteeLetter(id);
        return CommonResult.success(BeanUtil.copyProperties(guaranteeLetter, GuaranteeLetterRespVO.class));
    }

    @Operation(summary = "获得长期保函列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @GetMapping("/listAll")
    public CommonResult<List<GuaranteeLetterRespVO>> getGuaranteeLetterList(@RequestParam("ids") Collection<Long> ids) {
        List<GuaranteeLetterDO> list = guaranteeLetterService.getGuaranteeLetterList(ids);
        return CommonResult.success(BeanUtil.copyToList(list,GuaranteeLetterRespVO.class));
    }

    @Operation(summary = "获得长期保函分页")
    @PostMapping("/getPage")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:maintainLongTermGuarantee:search')")
    public CommonResult<PageResult<GuaranteeLetterRespVO>> getGuaranteeLetterPage(@Valid @RequestBody GuaranteeLetterPageReqVO pageVO) {
        PageResult<GuaranteeLetterDO> pageResult = guaranteeLetterService.getGuaranteeLetterPage(pageVO);
        List<GuaranteeLetterRespVO> results = BeanUtil.copyToList(pageResult.getList(),GuaranteeLetterRespVO.class);
        return CommonResult.success(new PageResult<>(results,pageResult.getTotal()));
    }
}
